home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Night Owl 6
/
Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso
/
033a
/
mxtms_10.zip
/
MISC.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-05-27
|
11KB
|
437 lines
/****************************************************************************/
/* */
/* Misc.c :Miscellaneous routines for MaxTime */
/* */
/****************************************************************************/
#include "MaxTime.h"
#include "globals.h"
int do_reset;
int CurColor;
int Hollercount=0;
int Bells;
int timeon(void)
{
clock_t elapsed;
int t;
elapsed = clock();
elapsed = startsecs - elapsed;
elapsed = elapsed / CLOCKS_PER_SEC; /* Compute seconds */
t = (int) (elapsed / 60);
if (t < 0)
t = t * (-1);
return(t);
}
void timeremain(void)
{
int x;
char tstr[18];
x = LastUser.timeremaining - timeon(); /* Fetch # of minutes on */
setcolor(TextAttr[TIME_LEFT]);
sprintf(tstr,"{%d min left} ",x);
strout(tstr);
}
void delay_s(unsigned n)
/* n = Number of seconds */
{
n *= 1000; /* Convert to seconds */
delay_ms(n);
}
void delay_ms(unsigned n)
/* milleseconds to delay */
{
struct timeb timebuf;
long end_time;
unsigned end_millitm;
/* get current time and calculate ending time */
ftime(&timebuf);
end_time = timebuf.time + (n / 1000);
end_millitm = timebuf.millitm + (n % 1000);
if (end_millitm >= 1000) {
++end_time;
end_millitm -= 1000;
}
/* loop until ending time reached */
do ftime(&timebuf);
while (timebuf.time < end_time ||
(timebuf.time == end_time && timebuf.millitm < end_millitm));
}
int find_realuser(char *name,int handle)
{
int x;
struct _usr USER;
lseek(handle,0L,SEEK_SET);
x = read(handle,(char *) &USER,user_slen);
while (x == user_slen) {
if (strcmpi(name,USER.name) == 0) {
if (USER.flag & UFLAG_deleted)
return(FALSE); /* It's there but it is deleted! */
else return(TRUE);
}
x = read(handle,(char *) &USER,user_slen);
}
return(FALSE); /* Couldn't find it */
}
/* Strip leading and trailing whitespace */
char *stripwhite(char *strng)
{
char *p,*pp;
while (*strng && isspace(*strng)) { /* Trim leading space */
pp = strng;
p = strng + 1;
while (*pp) { /* Shift up characters */
*pp = *p;
p++;
pp++;
}
}
if (!*strng) /* We have reached end of string */
return(NULL);
p = strng + (strlen(strng) - 1); /* Point to tail */
while (isspace(*p))
p--;
p[1] = 0; /* Terminate at start of trailing space */
return(strng);
}
/* Turn on given color attribute for video */
void setcolor(int color)
{
byte back,fore;
byte bit2,bit0;
char attstr[20];
fore = (byte) (color & 0x000f);
back = (byte) (color & 0x00070);
back = (byte) (back >> 4);
sprintf(attstr,"\x1b[1;%d;%dm",back+40,fore+30);
if (IsLocal) {
printf("\x1b[0m"); /* Reset attributes */
if (color & 0x0080) /* Test blink bit */
printf("\x1b[5m");
printf(attstr);
}
else {
if (LastUser.ansi) {
printf("\x1b[0m"); /* Reset attributes */
FossSendStr("\x1b[0m"); /* Reset attributes */
if (color & 0x0080) { /* Test blink bit */
printf("\x1b[5m");
FossSendStr("\x1b[5m");
}
printf(attstr);
FossSendStr(attstr);
}
if (LastUser.avatar) {
color |= 0x8; /* Turn on intensity for foreground */
printf(attstr); /* Print ANSI instead of avatar to local console */
/* Reverse bits 2 and 0 */
bit2 = (byte) (color & 0x04);
bit0 = (byte) (color & 0x01);
color &= 0xfa; /* Turn off bit 0 and bit 2 */
color |= (bit0 << 2);
color |= (bit2 >> 2);
sprintf(attstr,"\x16\x01%c",color);
FossSendStr(attstr); /* Send out attribute byte */
}
}
CurColor = color;
}
#define TIMERMODE 182 /* code to put timer in right mode */
#define FREQSCALE 1190000L /* basic time frequency in hertz */
#define TIMESCALE 1230L /* number of counts in 0.1 second */
#define T_MODEPORT 67 /* port controls timer mode */
#define FREQPORT 66 /* port controls tone frequency */
#define BEEPPORT 97 /* port controls speaker */
#define ON 79 /* signal to turn speaker on */
/* Generate a tone of given frequency and length, time is in 10ths of seconds */
void tone (int freq, int time)
{
int hibyte, lowbyte, port;
long divisor;
divisor = FREQSCALE / freq; /* scale freq to timer units */
lowbyte = (int) (divisor % 256); /* break integer into */
hibyte = (int) (divisor >> 8); /* two bytes */
outp (T_MODEPORT, TIMERMODE); /* prepare timer for input */
outp (FREQPORT, lowbyte); /* set low byte of timer reg */
outp (FREQPORT, hibyte); /* set high byte of timer reg */
port = inp (BEEPPORT); /* save port setting */
outp (BEEPPORT, ON); /* turn speaker on */
delay_ms(time);
outp (BEEPPORT, port); /* turn speaker off, restore */
/* original setting */
}
void strout(char *outmsg)
{
if (!IsLocal) {
check_carrier();
FossSendStr(outmsg);
check_carrier();
}
printf(outmsg);
}
void chrout(char ch)
{
putchar(ch);
if (!IsLocal)
FossSendCh(ch);
}
void strin(char *inmsg)
{
unsigned ch;
int key,minutes;
char *p;
p = inmsg;
cflush();
if (!IsLocal) { /* Simulate GETS function for fossil */
minutes = timeon(); /* Establish a counter */
while (1) {
check_carrier();
while ((ch = FossGetCh()) == 0xFFFF) {
check_carrier();
if (timeon() > minutes + IDLETIME) /* User is idle too long */
aborterror(USERIDLE,NULL);
}
key = ch & 0xff;
if (key == '\r') { /* Carraige return */
break;
}
if (key == '\b') { /* Backspace */
if (p > inmsg) {
FossSendCh((byte)key);
putchar(key);
FossSendCh(' ');
putchar(' ');
FossSendCh((byte) key);
putchar(key);
p--;
}
}
else {
(*p) = (char) key;
p++;
FossSendCh((byte) key);
putchar(key);
}
*p = 0; /* Terminate */
}
*(p+1) = 0;
}
else gets(inmsg);
}
int chrin(void)
{
unsigned ch;
int minutes;
cflush();
if (IsLocal) {
ch = _bios_keybrd(_KEYBRD_READ);
return(ch & 0xff); /* Return saying to restart menu */
}
else {
minutes = timeon(); /* Establish a counter */
while ((ch = FossGetCh()) == 0xFFFF) {
check_carrier();
if (timeon() > minutes + IDLETIME) { /* User is idle too long */
aborterror(USERIDLE,NULL);
}
}
return(ch);
}
}
int chrinwait(int tminutes)
{
unsigned ch;
int minutes;
cflush();
minutes = timeon(); /* Establish a counter */
if (IsLocal) {
while (_bios_keybrd(_KEYBRD_READY)) {
if (timeon() > minutes + tminutes) { /* User is idle too long */
return('\r');
}
}
ch = _bios_keybrd(_KEYBRD_READ);
return(ch & 0xff); /* Return saying to restart menu */
}
else {
while ((ch = FossGetCh()) == 0xFFFF) {
check_carrier();
if (timeon() > minutes + tminutes) { /* User is idle too long */
return('\r');
}
}
return(ch);
}
}
void cflush(void)
{
while (_bios_keybrd(_KEYBRD_READY)) /* Flush local console */
getch();
if (IsLocal)
return;
FossPurgeBuff(TRUE);
}
int check_carrier(void)
{
unsigned status;
if (IsLocal)
return(FALSE);
status = FossMdmStatus();
if ((status & 128) == 0) { /* Look at DCD(carrier bit) */
aborterror(DROPCARRIER,NULL);
}
return(FALSE);
}
void logit(char *strng,char flag)
{
FILE *log;
char ttemp[40];
char *ttemp1;
char *month;
char *day;
char *tm;
if (LogFile[0] == 0)
return;
ttemp1 = (char *) malloc(101);
if (ttemp1 == NULL) {
LogMode = FALSE;
aborterror(BADALLOC,NULL);
}
strcpy(ttemp, timestring());
strtok(ttemp," "); /* Strip Day of week */
month = strtok(NULL," ");
day = strtok(NULL," ");
tm = strtok(NULL," ");
if (Task)
sprintf(ttemp1,"%c %s %s %s MaxTime{%d} %s\n",flag,day,month,tm,Task,strng);
else sprintf(ttemp1,"%c %s %s %s MaxTime %s\n",flag,day,month,tm,strng);
if ((log = _fsopen(LogFile,"a",SH_DENYWR)) == NULL) {
printf("\a\aError opening log file!\n");
LogMode = FALSE;
free(ttemp1);
return;
}
fputs(ttemp1,log);
free(ttemp1);
fclose(log);
}
char *timestring(void)
{
time_t aclock;
struct tm *newtime;
time(&aclock);
newtime = localtime(&aclock);
return(asctime(newtime));
}
void signon(void)
{
char *p;
struct stat fbuf;
setcolor(TextAttr[HILITE_TEXT]);
/* Clear the screen */
if (LastUser.ansi)
strout("\x1b[2J");
else if (LastUser.avatar) {
printf("\x1b[2,J"); /* Always send ansi to local console */
FossSendStr("\x0c"); /* ^L */
}
sprintf(temp,"Begin, v%2.2f",Version);
logit(temp,'+');
if (!newuser)
sprintf(temp,"%s logging on",LastUser.name);
else sprintf(temp,"New User: %s logging on",LastUser.name);
logit(temp,':');
strout("MaxTime - a MAXIMUS timebank utility by Craig Derouen\r\n");
sprintf(temp,"\t *** Version %.2f ***\r\n",Version);
strout(temp);
setcolor(TextAttr[STD_TEXT]);
strcpy(temp1,LastUser.name);
p = strtok(temp1," "); /* Fetch 1st name only, shows how friendly we are */
if (USERCFG.calls) {
sprintf(temp,"\r\nWelcome back to MaxTime, %s.\r\n",p);
strout(temp);
}
else {
sprintf(temp,"\r\nWelcome to MaxTime, %s.\r\n",p);
strout(temp);
newuser_help(); /* Display a file to the new user */
}
/* Now test filedate on Bulletin file. If it is newer than last logon
of user, display it. */
if(stat(BullFile,&fbuf) == 0 ) { /* File does exist */
if (fbuf.st_mtime >= USERCFG.lasttime || (USERCFG.calls == 0)) { /* See if changed since last logon */
logit("User is viewing bulletin file",'#');
setcolor(TextAttr[STD_TEXT]);
strout("\r\nBulletins:\r\n");
if(show_file(BullFile) >= 0) {
setcolor(TextAttr[HILITE_TEXT]);
strout("\r\n\r\nPress any key to continue logon.");
chrinwait(1);
strout("\r\n");
}
}
}
}